package com.opo5.logisticsmanagement.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.opo5.logisticsmanagement.common.R;
import com.opo5.logisticsmanagement.dao.RoleDao;
import com.opo5.logisticsmanagement.dao.RoleMenuDao;
import com.opo5.logisticsmanagement.dao.UserDao;
import com.opo5.logisticsmanagement.domain.Role;
import com.opo5.logisticsmanagement.domain.RoleMenu;
import com.opo5.logisticsmanagement.domain.User;
import com.opo5.logisticsmanagement.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author lixin
* @description 针对表【tbs_role】的数据库操作Service实现
* @createDate 2022-07-10 08:55:41
*/
@Service
public class RoleServiceImpl extends ServiceImpl<RoleDao, Role>
    implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleMenuDao roleMenuDao;

    @Override
    public R getRoleByCondition(String name, Integer currentPage, Integer pageSize) {
        Page<Role> page = new Page<>(currentPage,pageSize);
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(name!=null,"name",name);
        roleDao.selectPage(page, queryWrapper);
        List<Role> roles = page.getRecords();
        return R.success(new R().add("roles",roles).add("total",page.getTotal()).getMap());
    }

    @Override
    public R getRoleById(Integer id) {
        Role role = roleDao.selectById(id);
        return R.success(role);
    }

    @Override
    public R addRole(Role role) {
        int flag = roleDao.insert(role);
        if (flag==1){
            return R.success(null);
        }
        return R.error("添加角色信息失败");
    }

    @Override
    public R editRole(Role role) {
        int flag = roleDao.updateById(role);
        if (flag==1){
            return R.success(null);
        }
        return R.error("修改角色信息失败");
    }

    @Override
    @Transactional
    public R delRole(Integer[] ids) {
        int i = 0;
        for(Integer id : ids){
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("role_id",id);
            Integer count = userDao.selectCount(queryWrapper);
            if (count.equals(0)){
                QueryWrapper<RoleMenu> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("role_id",id);
                roleMenuDao.delete(queryWrapper1);
                roleDao.deleteById(id);
                i++;
            }
        };
        if(i != ids.length){
            return R.error("删除了"+i+"条,"+"失败"+(ids.length-i)+"条");
        }
        return R.success(null);
    }

    @Override
    public List<Role> getRoleAll() {
        List<Role> roles = roleDao.selectList(null);
        return roles;
    }
}




